﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using JupiterLibrary;
using KinectXNATools;

namespace KinnectInput.KinectMetro
{
    public class KinectMetroIconButton:KinectMetroBtn
    {
        Sprite Icon;
        Point IconPos;
        Rectangle IconSize;
        Texture2D inActiveTex;
        Texture2D ActiveTex;
        public KinectMetroIconButton(Vector2 position, Rectangle r,Texture2D backGroundImage,Texture2D icon)
        {
            this.Position = position;
            this.Bounds=r;
            if (backGroundImage != null)
            {
                this.BackGroundImage = new Sprite();
                
                this.BackGroundImage.SetRectangle(this.Bounds);
                this.BackGroundImage.Texture = backGroundImage;
                this.inActiveTex = backGroundImage;
                Color [] ActiveColor=new Color[backGroundImage.Width*backGroundImage.Height];
                backGroundImage.GetData(ActiveColor);
                for(int i=0;i<ActiveColor.Length;i++){
                    ActiveColor[i].R+=10;
                    ActiveColor[i].G+=10;
                    ActiveColor[i].B+=10;
                }
                Texture2D tex=new Texture2D(MenuStyle.graphicsDevice,backGroundImage.Width,backGroundImage.Height);
                tex.SetData(ActiveColor);
                this.ActiveTex = tex;
            }
            if (icon != null)
            {
                Icon = new Sprite();
                Icon.Texture = icon;
                IconSize = icon.Bounds;
                IconPos = new Point(((r.Width / 2) - (icon.Bounds.Width / 2)), (r.Height / 2) - (icon.Bounds.Height / 2));
                IconSize.X = IconPos.X + XNAMathHelper.ToPoint(Position).X;
                IconSize.Y = IconPos.Y + XNAMathHelper.ToPoint(Position).Y;
                this.Icon.SetRectangle(IconSize);
                Icon.Update();
            }
        }
        public override void Build(KinectMetroButtonGrp parent)
        {
            this.Parent = parent;
            Point p = XNAMathHelper.ToPoint(Parent.Position + this.Position);
            if (BackGroundImage != null)
            {
                BackGroundImage.SetRectangle(new Rectangle(p.X, p.Y, Bounds.Width, Bounds.Height));
            }
            if (Icon != null)
            {
                Point pIcon = XNAMathHelper.ToPoint(Parent.Position + this.Position);
                Icon.SetRectangle(new Rectangle(pIcon.X, pIcon.Y, IconSize.Width, IconSize.Height));
                Icon.Update();
            }
            Parent.EnterArea += new ItemAreaEnterHandler(Parent_EnterArea);
            Parent.LeaveArea += new ItemAreaLeaveHandler(Parent_LeaveArea);
        }

        void Parent_LeaveArea(object sender, EventArgs e)
        {
            this.BackGroundImage.Texture = this.inActiveTex;
            this.BackGroundImage.Update();
        }

        void Parent_EnterArea(object sender, EnterAreaEventArgs e)
        {
            
                this.BackGroundImage.Texture = this.ActiveTex;
                this.BackGroundImage.Update();
            
        }
        public override void Update()
        {
            Point p=XNAMathHelper.ToPoint(Parent.Parent.Position+Parent.Position+this.Position);
            if (BackGroundImage != null)
            {
                Bounds = new Rectangle(p.X, p.Y, Bounds.Width, Bounds.Height);
                BackGroundImage.SetRectangle(new Rectangle(p.X, p.Y, Bounds.Width, Bounds.Height));
                BackGroundImage.Update();
            }
            if (Icon != null)
            {
                Vector2 offset = new Vector2((this.Bounds.Width / 2) - (this.IconSize.Width / 2), (this.Bounds.Height / 2) - (this.IconSize.Height / 2));
                Point pIcon = XNAMathHelper.ToPoint(Parent.Parent.Position + Parent.Position + this.Position+offset);
                Icon.SetRectangle(new Rectangle(pIcon.X, pIcon.Y, IconSize.Width, IconSize.Height));
                Icon.Update();
            }
            
        }
        
        public override void Draw()
        {
            if(BackGroundImage!=null)
                BackGroundImage.Draw();
            
            if(Icon !=null)
                Icon.Draw();
        }

    }
}
